<?php
/**
 * menu functionality
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: yui.menu.class.php 129 2010-11-09 09:00:53Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * Menu functionality for front-end
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class yuiMenu
{
    /**
     * @var String name of the menu
     */
    protected $s_MENUNAME = null;
    /**
     * @var Menu items
     */
    protected $a_MENU = array ();
    protected $s_menuMainNode = 'var aItemData[MENUNAME] = [[ITEMS]];';
    protected $s_menuItem = '{ text: \'[LINKTEXT]\', url: \'[LINKURL]\'[ONCLICK] }';
    protected $s_menuSubItem = '{text: \'[LINKTEXT]\', submenu: { id: \'[LINKTEXT]\', itemdata: [[ITEMS]] }}';
    protected $s_JS = 'var oMenu = new YAHOO.widget.MenuBar("[MENUNAME]",
        { autosubmenudisplay: false, keepopen: false, shadow: false, clicktohide: true,
        submenuhidedelay: 1000, hidedelay: 2250, showdelay:0, lazyload: false, itemdata: aItemData[MENUNAME] });
        oMenu.render("[RENDERTARGET]");';
    protected $s_menuBuild = null;
    protected $s_IDENT = null;
    protected $DEFAULT_ONCLICK = null;
    /**
     * quantity of menu entries
     *
     * @var int
     */
    protected $i_MENUCOUNT = 0;
    /**
     * create the root entry.
     *
     * @param string $menuname name of menu root entry
     *
     * @return void
     */
    public function __construct($menuname = 'undefined')
    {
        $this->s_MENUNAME = $menuname;
        $this->s_IDENT = md5($this->s_MENUNAME . serialize($this));
    }
    /**
     * quantity of menu entries
     *
     * @return int quantity of entries
     */
    function getMenuQuantity()
    {
        return $this->i_MENUCOUNT;
    }
    /**
     * add a menu entry or a branch.
     *
     * @param string|object $entryname menu entryname, or another object
     * @param string        $link      link target
     * @param string        $onclick   onclick function to execute
     * @param string        $onclick   icon to show at menu item
     *
     * @return bool
     */
    public function addMenuEntry($entryname = 'undefined', $link = '#', $onclick = null, $icon = null)
    {
        $this->a_MENU[] = array(
            'linktext' => $entryname,
            'linkurl' => $link,
            'onclick' => ($onclick ? $onclick : $this->DEFAULT_ONCLICK),
            'icon' => ($icon ? $icon : null)
        );
        $this->i_MENUCOUNT++;
        return true;
    }
    /**
     * add a default onclick handler (js function) for menu items
     *
     * @param string $fnName js function to execute on click
     *
     * @return void
     */
    public function addDefaultOnclickFunction ($fnName = null)
    {
        $this->DEFAULT_ONCLICK = $fnName;
        return;
    }
    /**
     * add a menu entry or a branch.
     *
     * @param string|object $entryname menu entryname, or another object
     * @param string        $link      link target
     * @param object        $o         yuiMenu object
     *
     * @return bool
     */
    public function addSubMenuEntry($entryname = 'undefined', $link = '#', yuiMenu $o = null)
    {
        $this->addMenuEntry($entryname, $link);
        $this->a_MENU[$this->getMenuQuantity()-1]['submenu'] = $o;
        return true;
    }
    /**
     * build the menu and returns it
     *
     * @param bool $b submenu rendering, default = false
     *
     * @return string
     */
    function renderMenu($b = false)
    {
        $a = array();
        foreach ($this->a_MENU as $array)
        {
            $icon = $array['icon'] ? '<img class="yuiMenuIcon" src="' . $array['icon'] . '" /><span class="yuiMenuIconText">' : '';
            $iconEnd = $array['icon'] ? '</span>' : '';
            if (isset($array['submenu']) && is_object($array['submenu']))
            {
                $a_replace = array (
                    '[LINKTEXT]' => $icon . $array['linktext'] . $iconEnd,
                    '[LINKURL]' => $array['linkurl'],
                    '[ONCLICK]' => ($array['onclick'] ? ', onclick: { fn: ' . $array['onclick'] . ' }' : '')
                );
                $a[] = str_replace(array_keys($a_replace), $a_replace, $array['submenu']->renderMenu(true));
                continue;
            }
            $a_replace = array (
                '[LINKTEXT]' => $icon . $array['linktext'] . $iconEnd,
                '[LINKURL]' => $array['linkurl'],
                '[ONCLICK]' => ($array['onclick'] ? ', onclick: { fn: ' . $array['onclick'] . ' }' : '')
            );
            $a[] = str_replace(array_keys($a_replace), $a_replace, $this->s_menuItem);
        }
        $return = str_replace('[ITEMS]', implode(",\r", $a), ($b ? $this->s_menuSubItem : $this->s_menuMainNode));
        $return = str_replace('[MENUNAME]', $this->s_IDENT, $return);
        return $return;
    }
    /**
     * return js code
     *
     * @param string $s_renderTarget target to render view in
     *
     * @return string js code
     */
    public function getJS($s_renderTarget = null)
    {
        $return = str_replace('[RENDERTARGET]', $s_renderTarget, $this->s_JS);
        $return = str_replace('[MENUNAME]', $this->s_IDENT, $return);
        return $return;
    }
}